---
layout: default
title: Update Spreadsheet
---

<!--These are JavaScript Libraries-->
<script type="text/javascript" src="https://839-studio.github.io/Novel-Coronavirus-Updates/master/js/jquery-latest.min.js"></script>
<script type="text/javascript" src="https://839-studio.github.io/Novel-Coronavirus-Updates/master/js/utility.js"></script>
<script type="text/javascript" src="https://839-studio.github.io/Novel-Coronavirus-Updates/master/js/cookies.js"></script>
<script type="text/javascript" src="https://839-studio.github.io/Novel-Coronavirus-Updates/master/js/tabletop.js"></script>
<script type="text/javascript" src="https://839-studio.github.io/Novel-Coronavirus-Updates/master/js/js-yaml.min.js"></script>
<script type="text/javascript" src="https://839-studio.github.io/Novel-Coronavirus-Updates/master/js/github.js"></script>

<h1>Update </h1>
<script type="text/javascript">

    // Reset the cookie manually
    //Cookies.expire('token');

    // URL: https://[user/org].github.io/[repo]/pull-spreadsheet/?key=your_key&worksheet=worksheet_name&token=your_personal_access_token&org=your_org&repo=your_repo&branch=your_branch

    // Incoming Variables append
    var $key = getUrlVar('key');
    var $resource = getUrlVar('worksheet');
    var $oAuthToken = getUrlVar('token');
    var $org = getUrlVar('org');
    var $repo = getUrlVar('repo');
    var $branch = getUrlVar('branch');

    var $yaml_store = "";

    if($oAuthToken !='')
      {
      // Setting a cookie value
      Cookies.set('token', $oAuthToken);
      // Set with expiration
      // Cookies.set('token', $oAuthToken, { expires: '01/01/2017' });
      }

    // Grab the token from cookie
    $oAuthToken = Cookies.get('token');

    $(document).ready( function() {
      Tabletop.init( { key: $key,
                       callback: showInfo,
                       wanted: [ $resource ],
                       debug: true } )
    })

    console.log($resource);

    function showInfo(data, tabletop) {

      $products = new Array();

      $("#table_info").text("We found the tables " + tabletop.model_names.join(", "));

      $.each( tabletop.sheets(), function(i, sheet) {
        $("#table_info").append("<p>" + sheet.name + " has " + sheet.column_names.join(", ") + "</p>");
      });

      $product = tabletop.sheets($resource).all();

      $count = 0;
      $total_count = $product.length;
      $.each( tabletop.sheets($resource).all(), function(i, service) {

          $p = {};
          $.each(service, function($key, $value) {
            //console.log($key + ' == ' + $value);
            $p[$key] = $value;
          });
          $products.push($p);

          $count++;
          //console.log($count + ' == ' + $total_count);
          if($count==$total_count)
            {
            $yaml_dump = jsyaml.dump($products);

            document.getElementById('source').value = $yaml_dump;

            // Grab the token from cookie
            $oAuthToken = Cookies.get('token');

            var github = new Github({token: $oAuthToken,auth: "oauth"});
            var repo = github.getRepo($org,$repo);

            repo.getTree($branch + '?recursive=true', function(err, tree) {

              $.each(tree, function(treeKey, treeValue) {

                $writepath = '_data/' + $resource + '.yaml';
                $path = treeValue['path'];
                $sha = treeValue['sha'];

                //console.log($path + ' == ' + $writepath);

                if($path==$writepath)
                  {
                  repo.writemanual('master',$writepath, $yaml_dump, 'Save', $sha, function(err) { });
                  console.log("writing " + $writepath);
                  //alert("saved " + $writepath);
                  }
                });
              });

            }
      });

    }

</script>
<textarea cols="10" rows="5" id="source" style="border: 1px solid #000; width: 100%; height: 350px;"></textarea>
